using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.Shared;
using CocaCola_Inventory_System.Reports;

namespace CocaCola_Inventory_System.Forms
{
    public partial class RecievingReport : Form
    {
        public int userid = -1;
        DB ds;
        string username = "";
        DataTable items;
        retrieve r;
        public RecievingReport(int userId)
        {
            InitializeComponent();
            ds = new DB();
            items = ds.Recieving;
            
        }

        public void AddItem(int id ,string name, string serial, DateTime pd)
        {
            if (items.Select("Id =" + id).Length > 0)
            {
                MessageBox.Show("Item already inserted");                
            }
            else
            items.Rows.Add(new object[] {id,name,serial,pd});
        }

        private void RecievingReport_Load(object sender, EventArgs e)
        {
            

            try
            {
                r = new retrieve(((CocaCola_Inventory_System.Forms.Container)this.MdiParent).con);   
                username = r.GetUserNameforuserid(userid);                
                this.Text = "Recieving Report for " + username;
                dataGridView1.DataSource = items;
                dataGridView1.Columns[0].Visible = false;
            }
            catch (Exception)
            {

                MessageBox.Show("Failed To Load Form Data");
                this.Close();
            }
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedCells.Count < 1)
                return;
            if (dataGridView1.SelectedCells[0].RowIndex < 0 || dataGridView1.SelectedCells[0].RowIndex >= dataGridView1.Rows.Count)
            {

                return;
            }
            int id = Convert.ToInt32(dataGridView1.SelectedCells[0].OwningRow.Cells[0].Value);
            DataRow[] rows = items.Select("id = " + id);
            if (rows.Length == 1)
                rows[0].Delete();

        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right)
                return;

            dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
        }

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right)
                return;

            dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            RecievingRep rp = new RecievingRep();
            ParameterFields fields = new ParameterFields();
            ParameterField field = new ParameterField();
            ParameterDiscreteValue va = new ParameterDiscreteValue();
            ParameterField field1 = new ParameterField();
            ParameterDiscreteValue va1 = new ParameterDiscreteValue();

            ReportForm f = new ReportForm();
            field.Name = "username";
            field1.Name = "Department";
            field.ParameterValueType = ParameterValueKind.StringParameter;
            
            va.Value = username+"  "+r.GetLoginforuserid(userid);
            va1.Value = r.GetDepartmentforuserid(userid);

            field.CurrentValues.Add(va);
            field1.CurrentValues.Add(va1);

            fields.Add(field);
            fields.Add(field1);
           
            rp.SetDataSource(ds);
            f.ReportViewer.ParameterFieldInfo = fields;
            f.ReportViewer.ReportSource = rp;
            f.ReportViewer.DisplayGroupTree = false;            
            f.MdiParent = this.MdiParent;
            f.Show();
            
        }
    }
}